<?php

namespace App\Models\video;

use App\Library\UpTools;
use Illuminate\Database\Eloquent\Model;

// 视频播放的列表
class PlayListModel extends Model
{
    protected $table = "up_play_list"; //表名
    const CREATED_AT = null;
    const UPDATED_AT = null;
    static $havingData;

    public static function transferField($data){
        if(!$data){
            return [];
        }
        $data = UpTools::transferFields($data);
        if(isset($data[0])){
            foreach ($data as $k => $item){
                if (!empty($item["createTime"])) {
                    $data[$k]["createTime"] = date("Y-m-d H:i", $item["createTime"]);
                }
                if (isset($item["broadcasterId"])) {
                    if(isset(self::$havingData[$item["broadcasterId"]])){
                        $data[$k]["broadcasterName"] = self::$havingData[$item["broadcasterId"]];
                    }else {
                        $data[$k]["broadcasterName"] = self::$havingData[$item["broadcasterId"]] = BroadcasterModel::getValue("name", ["id" => $item["broadcasterId"]]);
                    }
                }
            }
        }else {
            if (!empty($data["createTime"])) {
                $data["createTime"] = date("Y-m-d H:i", $data["createTime"]);
            }
            if (isset($data["broadcasterId"])) {
                $data["broadcasterName"] = BroadcasterModel::getValue("name", ["id" => $data["broadcasterId"]]);
            }
        }
        return $data;
    }

    // 分页获取所有字段数据
    public static function getList($field = '*', $filter = [], $page = 1, $limit = 10, $order = "create_time", $sort = "desc"){
        $db = self::query()->where($filter)->select(is_array($field) ? $field : explode(",", $field));
        $data['count'] = $db->count();
        $data['currentPage'] = $page;
        $data['totalPage'] = ceil(($data['count'])/$limit);
        $data['list'] = $db->skip(($page - 1) * $limit)->take($limit)->orderBy($order, $sort)->get();
        if($data['list']){
            $data['list'] = self::transferField($data['list']->toArray());
        }
        return $data;
    }

    //根据条件获取数量
    public static function getCount($filter){
        if(!empty($filter)){
            return self::query()->where($filter)->count();
        }else{
            return self::query()->count();
        }
    }

    //添加
    public static function toAdd($data, $getId=false){
        if(!$data)
            return true;
        $data["mall_id"] = request()->mallData["id"];
        if(empty($data["create_time"])) $data["create_time"] = time();
        if ($getId) {
            $res = self::query()->insertGetId($data);
        } else {
            $res = self::query()->insert($data);
        }
        if($res){
            PlayClassModel::doUpdate(["num" => self::query()->where("class_id", $data["class_id"])->where("mall_id", request()->mallData["id"])->count()], ["id" => $data["class_id"]]); // 更新分类下视频数
        }
        return $res;
    }

    //更新
    public static function doUpdate($data, $filter = []){
        if(!$data)
            return true;
        $res = self::query()->where($filter)->update($data);
        if($res || $res === 0){
            return true;
        }else{
            return false;
        }
    }

    //获取多字段数据
    public static function getFields($field, $filter = [], $one = true, $toArray = true){ //缓存方法：缓存所有字段，只返回需要的字段
        if($one){
            $data = self::query()->where($filter)->select(is_array($field) ? $field : explode(",", $field))->first();
            return $data ? ($toArray ? self::transferField($data->toArray()) : UpTools::transferFields($data->toArray())) : [];
        }else{
            $data = self::query()->where($filter)->select(is_array($field) ? $field : explode(",", $field))->get()->toArray();
            return $data ? ($toArray ? self::transferField($data) : UpTools::transferFields($data)) : [];
        }
    }

    //根据ID删除数据
    public static function delById($id, $data = null){
        if(!$data) {
            $data = self::getFields("class_id", ["id" => $id], true, false);
        }
        $result = self::query()->where('id', $id)->delete();
        if($result){
            PlayClassModel::doUpdate(["num" => self::query()->where("class_id", $data["classId"])->where("mall_id", request()->mallData["id"])->count()], ["id" => $data["classId"]]); // 更新分类下视频数
        }
        return $result;
    }
}
